<template>
	<view class="container">
		<view class="c-input">
			<view class="c-label">
				手机号
			</view>
			<input type="number" @input="onUserName" v-model="userName" placeholder="请输入手机号" />
		</view>
		<view class="c-input">
			<view class="c-label">
				密码
			</view>
			<input type="password" @input="onPassWord" v-model="passWord" placeholder="请输入密码" />
		</view>
		<view class="c-input">
			<button type="primary" class="c-input-btn" :disabled="blnDisabled" @click="onLogin">登录</button>
		</view>
		<view class="c-link">
			<navigator url="./forgetpwd">忘记密码</navigator>
			<navigator url="./register">注册</navigator>
		</view>
	</view>
</template>

<script>
	let pageApp = getApp();

	export default {
		data() {
			return {
				userName: uni.getStorageSync("LoginName"),
				passWord: "",
				blnDisabled: true
			}
		},
		methods: {
			GetDictionary() {
				// 准备数据
				let apiData = null;
				// 定义一个类型字典
				let _dicArr = ["leavetype","transporttype","leaves_status","travel_status","overtimetype","overtime_status","officialout_status"];
				// 遍历数据获取每一项
				_dicArr.forEach(item=>{
					apiData = {
						url: pageApp.globalData.url + "/GetDictionary",
						data: {
							"Type": item
						},
						success: (d) => {
							// 缓存类型字典
							uni.setStorageSync(item, JSON.stringify(d.data.data.items));
						}
					}
					// 发送数据请求
					this.vjAjaxData(apiData);
				})
			},
			onChkUserName() {
				if (this.userName.length > 0) {
					if (this.checkMobileCode(this.userName)) {
						uni.showToast({
							title: "格式有误！"
						})
						this.blnDisabled = true;
					}
				}
			},
			onUserName(e) {
				this.blnDisabled = this.checkIsEmpty(this.userName, this.passWord);
				this.userName = e.detail.value;
			},
			onPassWord(e) {
				this.blnDisabled = this.checkIsEmpty(this.userName, this.passWord);
				this.passWord = e.detail.value;
			},
			onLogin() {
				// 数据基础验证
				if (this.checkMobileCode(this.userName)) {
					uni.showToast({
						icon: "none",
						title: "手机号格式有误！"
					})
				} else {
					// 准备请求数据
					let apiData = {
						url: pageApp.globalData.url + "/Login",
						data: {
							"LoginName": this.userName,
							"PasswordSalt": this.passWord,
							"CheckCode": ""
						},
						method: "POST",
						success: (d) => {
							if (d.data.code == "001") {
								uni.showToast({
									icon: "none",
									title: "登录成功！"
								})
								// 保存用户令牌
								uni.setStorageSync("token", d.data.data.Token);
								// 保存用户数据
								uni.setStorageSync("userLoginData", JSON.stringify(d.data.data));
								// 保存用户名
								uni.setStorageSync("LoginName", this.userName);
								// 保存用户相关信息
								let userLoginInfo = {};
								userLoginInfo.CurrentOrgID = d.data.data.OrgID;
								userLoginInfo.CurrentOrgName = d.data.data.OrgName;
								userLoginInfo.CurrentDepartmentID = d.data.data.DepartmentID;
								uni.setStorageSync("userLoginInfo", JSON.stringify(userLoginInfo));
								// 获取类型字典
								this.GetDictionary();
								// 获取用户权限
								let askData = {
									url: pageApp.globalData.url + "/GetAuth",
									data: {},
									success: (d) => {
										if (d.data.code == "001") {
											uni.setStorageSync("userAuthInfo", JSON.stringify(d.data.data));
											uni.switchTab({
												url: '../home/msg/MsgUserList'
											});
										} else {
											uni.showToast({
												icon: "none",
												title: d.data.msg
											})
										}
									}
								}
								// 发送数据请求
								this.vjAjaxData(askData);
							} else {
								uni.showToast({
									icon: "none",
									title: d.data.msg
								})
							}
						}
					}
					// 发送数据请求
					this.vjAjaxData(apiData);
				}
			}
		}
	}
</script>

<style>

</style>
